package com.thc.dao;

import com.thc.domain.Sheet;
import com.thc.utils.DBConn;
import com.thc.utils.Fenye;
import com.thc.utils.Pager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SheetDao {
    public Map<String, List<Sheet>> search(  String name) {
        List<Sheet> list = new ArrayList<>();
        Sheet userInfo = null;
        String sql = "select * from sheet1 ";
        sql = sql + " where 标题 like '%" + name + "%'";
        sql=sql+" or 内容 like '%" + name + "%'";
        Connection conn = null;
        ResultSet rs;
        PreparedStatement ps = null;
        try {
            conn = DBConn.getConn();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery(sql);
            while (rs.next()) {
                userInfo = new Sheet();
                userInfo.setTitle(rs.getString("标题"));
                userInfo.setContent(rs.getString("内容"));
                userInfo.setPubdate(rs.getString("发布日期"));
                list.add(userInfo);
            }
        } catch (Exception e) {
            System.out.println("获取信息时发生了错误!");
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        Map<String,List<Sheet>> map=new HashMap<>();
        map.put("res",list);
        return map;
    }
}
